<?php
//KHONG DUNG NUA
class Auth_Admin extends MX_Controller
{
    private $_last_message = '';
    public function __construct()
    {
        parent::__construct();
        if (!modules::run('auth/auth/is_logged_in')) redirect('/');

    }

    function dispatcher($method=NULL, $para1=NULL, $para2=NULL)
    {
        $current_menu       = array();
        $layout             = 'powercms/admin_layout';

        switch($method)
        {
            case 'change_password' :
                $main_content           = $this->change_password();
                break;
        }

        $view_data                  = array();
        $view_data['admin_menu']    = modules::run('menus/menus/get_dashboard_menus');
        $view_data['main_content']  = $main_content;
        // META data
        $view_data['title']         = $this->_title;
        $this->load->view($layout, $view_data);
    }

    function get_last_message()
    {
        return $this->_last_message;
    }

    public function change_password()
    {
        $options = array();

        if ($this->is_postback())
            if (!$this->_process_change_password())
                $options['error'] = $this->_last_message;
            else
                $options['succeed'] = $this->_last_message;

        $options['base_url'] = base_url();
        $this->_title   = 'Thay đổi mật khẩu' . DEFAULT_TITLE_SUFFIX;

        return $this->load->view('auth/admin/change_password_form', array('options' => $options), TRUE);
    }

    private function _process_change_password()
    {
        $this->_last_message  = '';
        $options                    = array();

        $this->form_validation->set_rules('password', 'Mật khẩu cũ', 'trim|required|xss_clean');
        $this->form_validation->set_rules('new_password', 'Mật khẩu mới', 'trim|required|min_length[5]|xss_clean');
        $this->form_validation->set_rules('new_password2', 'Xác nhận mật khẩu mới', 'trim|required|min_length[5]|matches[new_password]|xss_clean');
        $this->form_validation->set_rules('security_code', 'Mã an toàn', 'trim|required|matches_value[' . $this->phpsession->get('captcha') . ']|xss_clean');

        if ($this->form_validation->run())
        {
            $options['id']              = $this->phpsession->get('user_id');
            $options['password']        = md5($this->input->post('password'));
            $options['new_password']    = md5($this->input->post('new_password'));
            $options['new_password2']   = md5($this->input->post('new_password2'));

            // check existance of new username
            $contact = $this->users_model->get_users(array('user_id' => $options['id']));
            if (count($contact) > 0)
            {
                if ($contact[0]->password===$options['password'])
                {
                    $this->users_model->update_user(array('id' => $options['id'], 'password' => $options['new_password']));
                    $this->_last_message = '<p>Bạn đã thay đổi mật khẩu thành công.</p>';
                    return TRUE;
                }
                else
                {
                    $this->_last_message = '<p>Mật khẩu cũ không chính xác.</p>';
                    return FALSE;
                }
            }

            $this->_last_message = $this->contact_model->get_last_message();
            return FALSE;
        }

        $this->_last_message = validation_errors();
        return FALSE;
    }
}
?>
